/**
 * 
 * @authors rick
 * @date    201703
 * @description 主入口模块
 */

import React from 'react'
import { render } from 'react-dom'

// 引入React-Router模块
import { Router, Route, Link, hashHistory, IndexRoute, Redirect, IndexLink} from 'react-router'

// 引入Antd的导航组件
import { Menu, Icon, Switch } from 'antd'
require('es6-promise').polyfill();
const SubMenu = Menu.SubMenu
// 引入Ant-Design样式 & Animate.CSS样式
import 'animate.css/animate.min.css'
import 'font-awesome/css/font-awesome.min.css'

// 引入主体样式文件
import './main.css'


import login from './components/login.js'
import sign from './components/sign.js'
import courseAdd from './components/course-manage/course-add.js'
import courseUpdate from './components/course-manage/course-update.js'
import course from './components/course-manage/course.js'
import courseNotic from './components/course-manage/course-notic.js'
import courseSign from './components/course-manage/course-sign.js'
import courseSignAdd from './components/course-manage/course-sign-add.js'
import courseSignResult from './components/course-manage/course-sign-result.js'
import courseExamine from './components/course-manage/course-examine.js'

import eleList from './components/ele-manage/ele-list.js'
import eleOperate from './components/ele-manage/ele-operate.js'

import studentCourse from './components/student-manage/course-student.js'
import studentCourseAddone from './components/student-manage/course-studentAddone.js'
import student  from './components/student-manage/student.js'
import studentUpdate from './components/student-manage/student-update.js'

import question from './components/question-manage/question.js'
import questionAdd from './components/question-manage/question-add.js'
import questionResult from './components/question-manage/question-result.js'
import questionDetial from './components/question-manage/question-detial.js'
import questionUpdate from './components/question-manage/question-update.js'
import questionDetailAdd from './components/question-manage/question-detial-add.js'
import questionDetailUpdate from './components/question-manage/question-detial-update.js'

import userRegister from './components/user-manage/user-register.js'

import wall from './components/wall-manage/wall.js'

import config from './assets/config';
// 配置导航
class Sider extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
            currentOpen:[],
            currentSelet:[],
            user: ''
        }
    }

    menuClick = (e) => {
        this.setState({
            current: e.key
        })
    }

    loginOut(target){
      if (target.key === 'loginOut' ) {
        sessionStorage.removeItem('user')
        window.location.href='#/login';
      }
    }

    componentWillMount() {
        if(sessionStorage.user){
          let current=config.changeCurrent(this.props.location.pathname.replace('/',''));
          this.setState({currentOpen:current[0],currentSelet:current[1], user:sessionStorage.user})  
        }else{
          window.location.href='#/login';
        }
    }
    render() {
        const {currentOpen, currentSelet, }=this.state;
        return (
            <div>
                <div id="leftMenu"> 
                    <img src='src/assets/images/logo.png' width="50" id="logo"/>
                    <Menu theme="dark"
                        onClick={this.menuClick}
                        style={{ width: 185 }}
                        defaultOpenKeys={currentOpen}
                        defaultSelectedKeys={currentSelet}
                        mode="inline"
                    >
                        <SubMenu key="sub1" title={<span><Icon type="appstore" /><span>课程管理</span></span>}>
                            <Menu.Item key="1"><Link to="/course">课程管理</Link></Menu.Item>
                            <Menu.Item key="2"><Link to="/courseNotic">课程通知</Link></Menu.Item>
                            <Menu.Item key="3"><Link to="/courseSign">签到管理</Link></Menu.Item>
                            <Menu.Item key="4"><Link to="/courseExamine">报名审核</Link></Menu.Item>
                        </SubMenu>
                        <SubMenu key="sub2" title={<span><Icon type="book" /><span>电子证书管理</span></span>}>
                            <Menu.Item key="5"><Link to="/eleList">查看证书</Link></Menu.Item>
                            <Menu.Item key="6"><Link to="/eleOperate">证书下载与打印</Link></Menu.Item>
                        </SubMenu>
                        <SubMenu key="sub3" title={<span><Icon type="solution" /><span>学员档案管理</span></span>}>
                            <Menu.Item key="7"><Link to="/studentCourse">学员管理</Link></Menu.Item>
                            <Menu.Item key="8"><Link to="/student">学员档案</Link></Menu.Item>
                        </SubMenu>
                        <SubMenu key="sub4" title={<span><Icon type="exception" /><span>调查问卷管理</span></span>}>
                            <Menu.Item key="9"><Link to="/question">查看问卷</Link></Menu.Item>
                        </SubMenu>
                        <SubMenu key="sub5" title={<span><Icon type="video-camera" /><span>互动墙</span></span>}>
                            <Menu.Item key="11"><Link to="/wall">课前聊天室</Link></Menu.Item>
                        </SubMenu>
                    </Menu>
                </div>
                <div id="rightWrap">
                    <Menu mode="horizontal" onSelect={this.loginOut.bind(this)}>
                        <SubMenu title={<span><Icon type="user" />{ this.state.user }</span>}>
                            <Menu.Item key="addUser"><Link to="/userRegister">添加管理员</Link></Menu.Item>
                            <Menu.Item key="loginOut">退出</Menu.Item>
                        </SubMenu>
                    </Menu>
                    <div className="right-box">
                        { this.props.children }
                    </div>
                </div>
            </div>
        )
    }
}


// 配置路由
render((
    <Router history={hashHistory} >
        <Route path="/" component={Sider}>
            <IndexRoute path="course" component={course} />
            <Route path="/course" component={course} />
            <Route path="/courseAdd" component={courseAdd} />
            <Route path="/courseUpdate" component={courseUpdate} />
            <Route path="/courseNotic" component={courseNotic} />
            <Route path="/courseSign" component={courseSign} />
            <Route path="/courseSignAdd" component={courseSignAdd} />
            <Route path="/courseSignResult" component={courseSignResult} />
            <Route path="/courseExamine" component={courseExamine} />
            <Route path="/eleList" component={eleList} />
            <Route path="/eleOperate" component={eleOperate} />
            <Route path="/student" component={student} />
            <Route path="/studentUpdate" component={studentUpdate} />
            <Route path="/studentCourse" component={studentCourse} />
            <Route path="/studentCourseAddone" component={studentCourseAddone} />
            <Route path="/questionAdd" component={questionAdd} />
            <Route path="/question" component={question} />
            <Route path="/questionDetial" component={questionDetial} />
            <Route path="/questionResult" component={questionResult} />
            <Route path="/questionUpdate" component={questionUpdate} />
            <Route path="/questionDetailAdd" component={questionDetailAdd} />
            <Route path="/questionDetailUpdate" component={questionDetailUpdate} />
            <Route path="/wall" component={wall} />
            <Route path="/userRegister" component={userRegister} />
        </Route>
        <Route path="/login" component={login} />
        <Route path="/sign" component={sign} />
    </Router>
), document.getElementById('app'));


